﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using connect;
using System.Data.SqlClient;
using System.Collections;

namespace Pharmacy_management_system
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
           
        }
        DataTable zjb = new DataTable();
        string zifuc = @"server=.;database=PASS;Integrated Security=True";
        List<string> ksm = new List<string>();
        ArrayList any = new ArrayList();
        DataTable dt=new DataTable();
        public static string cq;
        public String sqlyuj1,sqlyuj2,jiansuoma,ks="";
        public Form2(string a,string c,bool d,string e)
        {
            InitializeComponent();

            label1.Text = a;
            button1.Text = c;
            label2.Text = "";

            dt.Columns.Add("名称");
            dt.Columns.Add("检索码");
            dt.Columns.Add("代码");
            zjb.Columns.Add("名称");
            zjb.Columns.Add("检索码");
            zjb.Columns.Add("代码");
            if (a == "科室")
            {
                string dept = "";
                if (d) dept = "where type in ( '2','3','4') order by type "; else dept = "where type in ( '1','4') order by type ";
                jiansuoma = "py_code";
                sqlyuj1 = "select dept_name as '名称',py_code as '检索码',dept_sn as '代码' from dict_dept ";
                sqlyuj2 = dept;
                dt = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            }
            else if (a=="医生")
            {
                jiansuoma = "t.py_code";
                ks=Connect.baozhuang(Connect.yy(e, any), "t.dept_name");
                sqlyuj1 = "select t.doctor_name as '医生名字',t.py_code as '检索码',t.dept_name as '科室名称' from dict_doctor t  ";
                sqlyuj2="where 1=1 " +ks;
                dt = Connect.dubiao(zifuc, sqlyuj1,sqlyuj2 );
            }
            else if (a=="诊断")
            {
                jiansuoma = "py_code";
                sqlyuj1 = "select icd_name as '诊断名',icd_code as '诊断代码',py_code as '检索码' from dict_icd ";
                sqlyuj2 = "";
                dt = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            }
            else if (a == "药品")
            {
                jiansuoma = "yp_name";
                sqlyuj1 = "select DISTINCT yp_name as '药品名称', yp_code as '药品代码',yp_form as '药品种类' from dict_yp_base_info ";
                sqlyuj2 = "";
                dt = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            }
            else if (a == "手术名称")
            {
                jiansuoma = "operation_desc";
                sqlyuj1 = " select DISTINCT  operation_desc as '手术名称', operation_code as '手术编码',operation_desc as '手术名称',type=(CASE type when  '1'  then '甲状腺手术'  when  '2'  then '乳房手术' when  '3'  then '腹外疝手术'  when  '4'  then '髋关节置换术' when  '5'  then '膝关节手术'  when  '6'  then '子宫肌瘤切除术' when  '7'  then '介入手术'  else '其他手术' end) from dbo.dict_operation ";
                sqlyuj2 = "";
                dt = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            }
            this.dataGridView1.DataSource = dt;
            this.dataGridView2.DataSource = zjb;
        }

        //取消
        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //">"按钮
        private void button3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            string row1 = dataGridView1.Rows[i].Cells[0].Value.ToString();//获取datagridview1的第一列
            string row2 = dataGridView1.Rows[i].Cells[1].Value.ToString();//获取datagridview1的第2列
            string row3 = dataGridView1.Rows[i].Cells[2].Value.ToString();//获取datagridview1的第2列
            Boolean b = true;
            foreach (String s in ksm)
            {
                if (row1 == s)
                {
                    b = false;
                    break;
                }
            }
            if (b)
            {
                Connect.dtqh(row1, row2, row3, i, zjb, dt, 0);

                ksm.Add(row1);//将选中的结果放进集合中
                dataGridView1.DataSource = dt;//更新列表中的数据

                dataGridView2.DataSource = zjb;//更新列表数据
            }
            
            //dataGridView1.Rows[dataGridView1.CurrentRow.Index].;
            }

        //">>"按钮
        private void button2_Click(object sender, EventArgs e)
        {
            dt.Clear();
            zjb.Clear();//清空两个列表中的数据
            
            zjb = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            this.dataGridView1.DataSource = dt;
            this.dataGridView2.DataSource = zjb;//重新再加载数据
        }
        //"<"按钮
        private void button4_Click(object sender, EventArgs e)
        {
            int i = dataGridView2.CurrentRow.Index;
            string row1 = dataGridView2.Rows[i].Cells[0].Value.ToString();
            string row2 = dataGridView2.Rows[i].Cells[1].Value.ToString();
            string row3 = dataGridView2.Rows[i].Cells[2].Value.ToString();
            Connect.dtqh(row1, row2, row3, i, dt, zjb, 1);
            ksm.Remove(row1);
            dataGridView1.DataSource = dt;
            dataGridView2.DataSource = zjb;//将第二列表中数据返还给第一列表
            
        }
        //"<<"按钮
        private void button5_Click(object sender, EventArgs e)
        {
            dt.Clear();
            zjb.Clear();
            ksm.Clear();
            dt = Connect.dubiao(zifuc, sqlyuj1, sqlyuj2);
            this.dataGridView1.DataSource = dt;
            this.dataGridView2.DataSource = zjb;
        }
        
        //"筛选"按钮
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != null && textBox1.Text.Trim() != "")
            {
                string a = "where "+jiansuoma+" like '%" + textBox1.Text.Trim() + "%'";
                dt = Connect.dubiao(zifuc, sqlyuj1, a);
                this.dataGridView1.DataSource = dt;
            }

        }

        //"确定"按钮
        private void button6_Click(object sender, EventArgs e)
        {
            foreach (string i in ksm)
            {
                cq += i + ",";
            }
            Form1.zuihoushuju = cq;
            cq = "";
            this.Close();
           
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            button3_Click(sender, e);
        }

        private void dataGridView2_DoubleClick(object sender, EventArgs e)
        {
            button4_Click(sender, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != null && textBox1.Text.Trim() != "")
            {
                string a= "where " + jiansuoma + " like '%" + textBox1.Text.Trim() + "%' "+ks;
                dt = Connect.dubiao(zifuc, sqlyuj1, a);
                this.dataGridView1.DataSource = dt;
            }
            else if (textBox1.Text.Trim()=="")
            {
                dt = Connect.dubiao(zifuc, sqlyuj1,"where 1=1 "+ks);
                this.dataGridView1.DataSource = dt;
            }

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                NewMethod("py_code", "t.py_code", "py_code", "py_code", "py_code");
            }
        }

        private void NewMethod(string a,string b,string c,string d,string e)
        {
            if (label1.Text == "科室")
                jiansuoma = a;
            else if (label1.Text == "医生")
                jiansuoma = b;
            else if (label1.Text == "诊断")
                jiansuoma = c;
            else if (label1.Text == "药品")
                jiansuoma = d;
            else if (label1.Text == "手术名称")
                jiansuoma = e;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            NewMethod("wb_code", "t.wb_code", "wb_code", "wb_code", "wb_code");
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            NewMethod("dept_name", "t_doctor", "icd_name", "yp_name", "operation_desc");
        }
        
        }

       

}
